<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\data\circle;

use app\api\model\dao\Note;
use app\api\model\dao\Circle;
use app\api\model\dao\NotePraise;
use app\api\model\dao\NoteReply;
use app\api\model\dao\NoteStatistics;
use app\api\model\dao\CircleStatistics;
use app\api\model\dao\UserAccount;
use app\api\model\dao\UserFollow;
use app\library\Flag;

use think\Log;

class CircleData
{

    private $objNote;
    private $objCircle;
    private $objUserAccount;
    private $objNotePraise;
    private $objNoteReply;
    private $objNoteStatistics;
    private $objCircleStatistics;
    private $objUserFollow;

    public function __construct()
    {
        $this->objNote = new Note();
        $this->objCircle = new Circle();
        $this->objUserAccount = new UserAccount();
        $this->objNotePraise = new NotePraise();
        $this->objNoteReply = new NoteReply();
        $this->objNoteStatistics = new NoteStatistics();
        $this->objCircleStatistics = new CircleStatistics();
        $this->objUserFollow = new UserFollow();
    }

    /**
     * 根绝圈子分类获取圈子列表
     *
     * @param int $cate_id
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function getCircleListByCategory($cate_id, $offset = 0, $limit = 10)
    {
        $arrCond = [
            'category_id' => ['=', $cate_id],
            'approve_status' => ['=', Flag::CONST_CIRCLE_APPROVE_PASS],
            'is_delete' => ['=', Flag::CONST_NOTE_DELETE_NOT],
        ];
        $retCircle = $this->objCircle->getCircle($arrCond, [], $offset, $limit, ' create_time desc ');
        if (false === $retCircle) {
            Log::error(__METHOD__ . ' get circle list fail.');
            return [];
        }

        return $this->composeCircle($retCircle);
    }

    /**
     * 我关注的圈子
     *
     * @param int $cir_id
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function followCircle($userid, $offset = 0, $limit = 10)
    {
        $arrCond = [
            'userid' => ['=', $userid],
            'follow_type' => ['=', Flag::CONST_FOLLOW_CIRCLE],
        ];
        $retUserFollow = $this->objUserFollow->getFollow($arrCond, [], $offset, $limit);
        if (false === $retUserFollow) {
            Log::error(__METHOD__ . ' get user_follow list fail.');
            return [];
        }
        $cir_id = [];
        foreach ($retUserFollow as $user_account) {
            $cir_id[] = $user_account['follow_id'];
        }

        if (empty($cir_id)) {
            return [];
        }

        $arrCond = [
            'id' => ['in', $cir_id],
            'approve_status' => ['=', Flag::CONST_CIRCLE_APPROVE_PASS],
            'is_delete' => ['=', Flag::CONST_NOTE_DELETE_NOT],
        ];
        $retCircle = $this->objCircle->getCircle($arrCond);
        if (false === $retCircle) {
            Log::error(__METHOD__ . ' get circle list fail.');
            return [];
        }

        return $this->composeCircle($retCircle);
    }

    /**
     * 最热圈子
     * 进入详情页PV量排序
     *
     * @param int $cir_id
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function hotCircle($offset = 0, $limit = 10)
    {
        $circleStatistics_ret = $this->objCircleStatistics->getCircleStatistics([], [], $offset, $limit, ' pv desc ');
        if (empty($circleStatistics_ret)) {
            return [];
        }
        $circle_ids = [];
        foreach ($circleStatistics_ret as $circle_statistics) {
            $circle_ids[] = $circle_statistics['cir_id'];
        }
        if (empty($circle_ids)) {
            return [];
        }
        $arrCond = [
            'id' => ['in', $circle_ids],
            'approve_status' => ['=', Flag::CONST_CIRCLE_APPROVE_PASS],
            'is_delete' => ['=', Flag::CONST_NOTE_DELETE_NOT]
        ];
        $retCircle = $this->objCircle->getCircle($arrCond);
        if (false === $retCircle) {
            Log::error(__METHOD__ . ' get circle list fail.');
            return [];
        }

        return $this->composeCircle($retCircle);
    }

    /**
     * 最新帖子
     *
     * @param int $cir_id
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function newCircle($cate_id = 0, $offset = 0, $limit = 10)
    {
        $arrCond = [
            'approve_status' => ['=', Flag::CONST_CIRCLE_APPROVE_PASS],
            'is_delete' => ['=', Flag::CONST_NOTE_DELETE_NOT],
        ];
        if ($cate_id) {
            $arrCond['category_id'] = ['=', $cate_id];
        }
        $retCircle = $this->objCircle->getCircle($arrCond, [], $offset, $limit, ' create_time desc ');
        if (false === $retCircle) {
            Log::error(__METHOD__ . ' get circle list fail.');
            return [];
        }

        return $this->composeCircle($retCircle);
    }

    /**
     * 我的圈子
     *
     * @param int $cir_id
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function mineCircle($userid = 0, $offset = 0, $limit = 10)
    {
        $arrCond = [
            'approve_status' => ['=', Flag::CONST_CIRCLE_APPROVE_PASS],
            'is_delete' => ['=', Flag::CONST_NOTE_DELETE_NOT],
        ];
        if ($userid) {
            $arrCond['creator'] = ['=', $userid];
        }
        $retCircle = $this->objCircle->getCircle($arrCond, [], $offset, $limit, ' create_time desc ');
        if (false === $retCircle) {
            Log::error(__METHOD__ . ' get circle list fail.');
            return [];
        }

        return $this->composeCircle($retCircle);
    }

    /**
     * 根据id获取圈子
     *
     * @param int $cir_id
     * @return array
     */
    public function getCircleByid($cir_id = 0, $offset = 0, $limit = 10)
    {
        $arrCond = [
            'id' => ['=', $cir_id],
        ];
        $retCircle = $this->objCircle->getCircle($arrCond);
        if (false === $retCircle) {
            Log::error(__METHOD__ . ' get circle list fail.');
            return [];
        }

        return $this->composeCircle($retCircle);
    }

    /**
     * 组装圈子所有数据
     *
     * @param $arrNote
     * @return array
     */
    public function composeCircle($arrCircle)
    {
        if (empty($arrCircle)) {
            return [];
        }

        $cir_id = [];
        foreach ($arrCircle as $circle) {
            $cir_id[] = $circle['id'];
        }

        $arrCirid2PV = [];
        if ($cir_id) {
            $arrCond['cir_id'] = ['in', $cir_id];
            $circleStatistics_ret = $this->objCircleStatistics->getCircleStatistics($arrCond);
            if (!empty($circleStatistics_ret)) {
                foreach ($circleStatistics_ret as $circle_statistics) {
                    if (isset($arrCirid2PV[$circle_statistics['cir_id']]) && !empty($arrCirid2PV[$circle_statistics['cir_id']])) {
                        $arrCirid2PV[$circle_statistics['cir_id']] += $circle_statistics['pv'];
                    } else {
                        $arrCirid2PV[$circle_statistics['cir_id']] = $circle_statistics['pv'];
                    }
                }
            }
        }

        $data = [];
        foreach ($arrCircle as $circle) {
            $label = explode(',', $circle['label']);
            $data[] = [
                'cir_id' => $circle['id'],
                'cate_id' => $circle['category_id'],
                'name' => $circle['name'],
                'icon' => $circle['icon'] ? $circle['icon'] : Flag::CONST_CIRCLE_DEFAULT_ICON,
                'pv' => isset($arrCirid2PV[$circle['id']]) && !empty($arrCirid2PV[$circle['id']]) ? $arrCirid2PV[$circle['id']] : 0,
                'word' => empty($circle['label']) ? [] : $label
            ];
        }
        return $data;
    }
}
